//
//  JHSpecialXQViewController.m
//  Liberty Voice
//
//  Created by dlios on 15-5-13.
//  Copyright (c) 2015年 ClearLove. All rights reserved.
//

#import "JHSpecialXQViewController.h"
#import "JHSpcialXQTableViewCell.h"
#import "NetwordHandler.h"
#import "JHSpecialXQ.h"
#import "UIImageView+WebCache.h"

@interface JHSpecialXQViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UIScrollView *XQscroll;
@property(nonatomic,retain)UITableView *XQtable;
@property(nonatomic,assign)CGFloat f1;
@property(nonatomic,assign)CGFloat f2;

@property(nonatomic,retain)NSMutableArray *XQArr;
@property(nonatomic,retain)NSMutableDictionary *DIc;
@end

@implementation JHSpecialXQViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
//    [self createScroll];
    [self getData];
}

-(void)getData
{
    NSString *s = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/subject_detail?device=android&id=%@",self.str];
    [NetwordHandler requestWithURL:s completion:^(id result) {
        NSLog(@"%@",result);
        NSMutableDictionary *d = result;
        self.DIc = [NSMutableDictionary dictionary];
        _DIc = [d objectForKey:@"info"];
        NSMutableArray *a = [d objectForKey:@"list"];
        self.XQArr = [NSMutableArray array];
        for (NSDictionary *d2 in a) {
            JHSpecialXQ *xq = [[JHSpecialXQ alloc] initWithDic:d2];
            [_XQArr addObject:xq];
        }
        [self createScroll];
    }];
}

- (void)createScroll
{
    self.title = [_DIc objectForKey:@"title"];
    self.XQscroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 20)];

//    _XQscroll.backgroundColor= [UIColor blackColor];
//    _XQscroll.pagingEnabled = YES;
    [self.view addSubview:_XQscroll];
    UIImageView *title = [[[UIImageView alloc] init] autorelease];
    title.frame = CGRectMake(0, 10, self.view.frame.size.width, self.view.frame.size.height / 4);
    [title sd_setImageWithURL:[NSURL URLWithString:[_DIc objectForKey:@"coverPathBig"]]];
    [_XQscroll addSubview:title];
    UILabel *titleLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, title.frame.size.height +10, self.view.frame.size.width, 30)] autorelease];
    titleLabel.textAlignment = 1;
    titleLabel.text = [_DIc objectForKey:@"title"];
    titleLabel.backgroundColor = [UIColor yellowColor];
    [_XQscroll addSubview:titleLabel];
    
    
    UILabel *label = [[UILabel alloc] init];
    label.text = [_DIc objectForKey:@"intro"];
    label.numberOfLines = 0;
    [label sizeToFit];
    label.font = [UIFont systemFontOfSize:14];
    label.textAlignment = 1;
    self.f2 = [JHSpecialXQViewController heightWithText:label.text];
    label.frame = CGRectMake(0, titleLabel.frame.origin.y + 30, self.view.frame.size.width, _f2 + 10);
    
    label.backgroundColor = [UIColor redColor];
    [_XQscroll addSubview:label];
    
    self.XQtable = [[[UITableView alloc] initWithFrame:CGRectMake(0, label.frame.origin.y + label.frame.size.height, self.view.frame.size.width, _XQArr.count * 120)] autorelease];
    _XQtable.delegate = self;
    _XQtable.dataSource= self;
    [_XQtable registerClass:[JHSpcialXQTableViewCell class] forCellReuseIdentifier:@"XQtable"];
    [_XQscroll addSubview:_XQtable];
    
    _XQscroll.contentSize = CGSizeMake(0,   _XQtable.contentSize.height + 40 +title.frame.size.height + label.frame.size.height );
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JHSpcialXQTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XQtable"];
    cell.specialXQ = _XQArr[indexPath.row];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _XQArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JHHotMucis *s = [[JHHotMucis alloc ]init];
    self.f1 = [JHSpcialXQTableViewCell heightWithText:s.title];
    return 110;
}
+ (CGFloat)heightWithText:(NSString *)text
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};
    CGSize size = CGSizeMake(360, 1000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
